package com.finance.quartz;

import com.finance.framework.qurtz.RunningStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by CBWL on 2017/6/5.
 */
@Component
public class RunningStatusDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public RunningStatus getByTrigger(String triggerName,String triggerGroup){
        String sql = "select trigger_name,trigger_group,running_status,create_time,update_time from qrtz_running_status where trigger_name=? and trigger_group=?";
        RunningStatus runningStatus = (RunningStatus) this.jdbcTemplate.queryForObject(sql, new Object[]{triggerName,triggerGroup}, new RunningStatusMapper());
        return runningStatus;
    }
    public void update(final String triggerName,final String triggerGroup,final byte[] runningStatus){
        String sql = "UPDATE qrtz_running_status SET running_status = ? WHERE trigger_name=? and trigger_group=?";
        this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                ByteArrayInputStream inputStream =new ByteArrayInputStream(runningStatus);
                preparedStatement.setBlob(1,inputStream);
                preparedStatement.setString(2,triggerName);
                preparedStatement.setString(3,triggerGroup);
            }
        });
     }
    public void save(final String triggerName,final String triggerGroup,final byte[] runningStatus){
        String sql = "insert into qrtz_running_status(trigger_name,trigger_group,running_status) values(?,?,?)";
        this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                ByteArrayInputStream inputStream =new ByteArrayInputStream(runningStatus);
                preparedStatement.setString(1,triggerName);
                preparedStatement.setString(2,triggerGroup);
                preparedStatement.setBlob(3,inputStream);
            }
        });
     }
     public void remove(final RunningStatus runningStatus){
         String sql = "DELETE from qrtz_running_status WHERE trigger_name=? and trigger_group=?";
         this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
             @Override
             public void setValues(PreparedStatement preparedStatement) throws SQLException {
                 preparedStatement.setString(1,runningStatus.getTriggerName());
                 preparedStatement.setString(2,runningStatus.getTriggerGroup());
             }
         });
     }
     public List<RunningStatus> listAll(){
         String sql = "select trigger_name,trigger_group,running_status,create_time,update_time from qrtz_running_status";
         List<RunningStatus> list = this.jdbcTemplate.query(sql,new RunningStatusMapper());
         return list;
     }
     class RunningStatusMapper implements RowMapper<RunningStatus>{

         @Override
         public RunningStatus mapRow(ResultSet resultSet, int i) throws SQLException {
             RunningStatus runningStatus = new RunningStatus();
             runningStatus.setTriggerName(resultSet.getString("trigger_name"));
             runningStatus.setTriggerGroup(resultSet.getString("trigger_group"));
             runningStatus.setRunningStatus(resultSet.getBytes("running_status"));
             runningStatus.setCreateTime(resultSet.getDate("create_time"));
             runningStatus.setUpdateTime(resultSet.getDate("update_time"));
             return runningStatus;
         }
     }
}
